package com.zoffy.wmsbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoffy.wmsbackend.common.RequestParam;
import com.zoffy.wmsbackend.model.dto.cost.CostDTO;
import com.zoffy.wmsbackend.model.entity.Cost;
import com.zoffy.wmsbackend.model.vo.CostVO;
import com.zoffy.wmsbackend.mapper.CostMapper;
import com.zoffy.wmsbackend.service.CostService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class CostServiceImpl extends ServiceImpl<CostMapper, Cost> implements CostService {
    @Resource
    private CostMapper costMapper;

    @Override
    public Page<CostVO> listCostPage(RequestParam<CostDTO> requestParam) {
        Integer pageNum = requestParam.getPageNum();
        Long pageSize = requestParam.getPageSize();
        CostDTO dto = requestParam.getParams();
        Page<CostVO> costPage = costMapper.listCostVo(new Page<>(pageNum, pageSize),dto);
        return costPage;
    }

    @Override
    public Boolean addCost(CostDTO dto) {
        Long warehouseId = dto.getWarehouseId();
        Long manufacturerId = dto.getManufacturerId();
        String money = dto.getMoney();
        Cost cost = new Cost();
        cost.setWarehouseId(warehouseId);
        cost.setManufacturerId(manufacturerId);
        cost.setMoney(money);
        return this.save(cost);
    }

    @Override
    public Boolean updateCost(CostDTO dto) {
        Long id = dto.getId();
        Long warehouseId = dto.getWarehouseId();
        Long manufacturerId = dto.getManufacturerId();
        String money = dto.getMoney();
        Cost cost = this.getById(id);
        if (cost == null) {
            return false;
        }
        if (warehouseId != null) {
            cost.setWarehouseId(warehouseId);
        }
        if (manufacturerId != null) {
            cost.setManufacturerId(manufacturerId);
        }
        if (StringUtils.isNotBlank(money)) {
            cost.setMoney(money);
        }
        return this.updateById(cost);
    }

    @Override
    public Boolean deleteCost(CostDTO dto) {
        return this.removeById(dto.getId());
    }
}